<template>
  <div class="container">
    <el-row>
      <el-card shadow="never">
        <div slot="header">
          <el-form :inline="true" :model="searchForm" size="small">
            <el-form-item label="ID" class="el-search-item">
              <el-input v-model="searchForm.sysParameterId" placeholder="ID" clearable></el-input>
            </el-form-item>
            <el-form-item label="创建时间" class="el-search-item">
              <el-date-picker v-model="checkedTime" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item class="el-search-item">
              <el-button type="success" plain icon="el-icon-search" @click="getDataList">查询</el-button>
            </el-form-item>
            <el-form-item class="el-search-item">
              <el-button v-if='hasPerm("sys:sysParameter:add")' type="primary" plain icon="el-icon-plus" @click="openAdd">新增</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!--数据表格 element ui-->
        <el-table :data="tableData" :height="tableHeight" style="width: 100%">
          <el-table-column prop="sysParameterId" label="id" align="center" sortable></el-table-column>
          <el-table-column prop="sysCompanyId" label="企业id" align="center" sortable></el-table-column>
          <el-table-column prop="parameterName" label="名称" align="center" sortable></el-table-column>
          <el-table-column prop="dataValue" label="内容 男=1,女=2" align="center" sortable></el-table-column>
          <el-table-column prop="parameterType" label="参数类型 1购物 2房产  3招聘 4消息" align="center" sortable></el-table-column>
          <el-table-column prop="dataType" label="类型1单选框  2复选框 3下拉框 4值" align="center" sortable></el-table-column>
          <el-table-column prop="msg" label="备注" align="center" sortable></el-table-column>
          <el-table-column prop="numb" label="编码" align="center" sortable></el-table-column>
          <el-table-column prop="seq" label="排序" align="center" sortable></el-table-column>
          <el-table-column prop="deleted" label="" align="center" sortable></el-table-column>
          <el-table-column v-if='hasPerm("sys:sysParameter:list")' label="更多操作" align="center" width="250">
            <template slot-scope="scope">
              <el-button v-if='hasPerm("sys:sysParameter:edit")' @click="openUpdate(scope.row)" size="mini" type="text" icon="el-icon-edit">编辑</el-button>
              <el-button v-if='hasPerm("sys:sysParameter:del")' @click="deleteFun(scope.row)" size="mini" type="text" icon="el-icon-delete">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <el-pagination style="padding:0;text-align: center;" background @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes, prev, pager, next, jumper"
                       :current-page="pageNum" :page-sizes="[8, 15, 20]" :page-size="pageSize" :total="total">
        </el-pagination>
      </el-card>
      <!--新增用户对话框-->
      <el-dialog :title="dialog.dialogTitle" :visible.sync="dialog.visible" width="35%">
        <el-form :rules="rules" ref="sysParameterForm" :model="sysParameterForm" size="small" label-width="80px">
          <el-form-item prop="sysParameterId" label="id">
            <el-input v-model="sysParameterForm.sysParameterId" placeholder="请输入id" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="sysCompanyId" label="企业id">
            <el-input v-model="sysParameterForm.sysCompanyId" placeholder="请输入企业id" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="parameterName" label="名称">
            <el-input v-model="sysParameterForm.parameterName" placeholder="请输入名称" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="dataValue" label="内容 男=1,女=2">
            <el-input v-model="sysParameterForm.dataValue" placeholder="请输入内容 男=1,女=2" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="parameterType" label="参数类型 1购物 2房产  3招聘 4消息">
            <el-input v-model="sysParameterForm.parameterType" placeholder="请输入参数类型 1购物 2房产  3招聘 4消息" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="dataType" label="类型1单选框  2复选框 3下拉框 4值">
            <el-input v-model="sysParameterForm.dataType" placeholder="请输入类型1单选框  2复选框 3下拉框 4值" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="msg" label="备注">
            <el-input v-model="sysParameterForm.msg" placeholder="请输入备注" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="numb" label="编码">
            <el-input v-model="sysParameterForm.numb" placeholder="请输入编码" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="seq" label="排序">
            <el-input v-model="sysParameterForm.seq" placeholder="请输入排序" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="deleted" label="">
            <el-input v-model="sysParameterForm.deleted" placeholder="请输入" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialog.visible = false" size="small">取 消</el-button>
          <el-button type="primary" @click="submitFun()" :loading="loading.status" size="small">{{loading.status ? loading.SubText : loading.BtnText}}</el-button>
        </span>
      </el-dialog>
    </el-row>
  </div>
</template>
<script>
  export default {//vue3
    data() {
      return {
        // 表格数据
        tableData: [],
        // 分页数据
        total: 0,
        pageNum: 1,
        pageSize: 8,
        // 表格高度
        tableHeight: 0,
        // 用来存放日期组件所选中的值
        checkedTime: '',
        // 查询参数
        searchForm: {sysParameterId: null, startTime: null, endTime: null,},
        // 弹出层
        dialog: {flag: 0, dialogTitle: "", visible: false},
        // 菜单弹出层
        menuDialog: {dialogTitle: "", visible: false},
        // 表单参数
        sysParameterForm: {
          sysParameterId: null,
          sysCompanyId: null,
          parameterName: null,
          dataValue: null,
          parameterType: null,
          dataType: null,
          msg: null,
          numb: null,
          seq: null,
          deleted: null,
        },
        // 提交按钮文字
        loading: {status: false,BtnText: '确 定',SubText: '提交中...'},
        // 表单校验
        rules: {
          sysParameterId: [{required: true,trigger: "change",message: "请输入id"}],
          sysCompanyId: [{required: true,trigger: "change",message: "请输入企业id"}],
          parameterName: [{required: true,trigger: "change",message: "请输入名称"}],
          dataValue: [{required: true,trigger: "change",message: "请输入内容 男=1,女=2"}],
          parameterType: [{required: true,trigger: "change",message: "请输入参数类型 1购物 2房产  3招聘 4消息"}],
          dataType: [{required: true,trigger: "change",message: "请输入类型1单选框  2复选框 3下拉框 4值"}],
          msg: [{required: true,trigger: "change",message: "请输入备注"}],
          numb: [{required: true,trigger: "change",message: "请输入编码"}],
          seq: [{required: true,trigger: "change",message: "请输入排序"}],
          deleted: [{required: true,trigger: "change",message: "请输入"}],
        },
        // 所有权限数据
        treeData: [],
        // 树形控件的属性绑定对象
        treeProps: {children: 'children', label: 'label'},
        // 默认选中的节点id值
        defKeys: [],
        // 角色id
        roleId: null
      };
    },
    mounted() {
      this.$nextTick(() => {
        this.tableHeight = window.innerHeight - 205;
      });
    },
    created() {
      this.getDataList();
    },
    watch: {
      "checkedTime"(value) {
        this.searchForm.startTime = null;
        this.searchForm.endTime = null;
        if (value != null) {
          let timeArr = value.toString().split(",");
          timeArr.forEach((item,i) => {
            if(i === 0) {
              this.searchForm.startTime = item;
            }else {
              this.searchForm.endTime = item;
            }
          });
        }
      }
    },
    methods: {
      handleSizeChange(val) {
        this.pageSize = val
        this.getDataList()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.getDataList()
      },
      // 获取列表
      getDataList() {
        this.$api.sys_sysParameter.list({
          current: this.pageNum,
          size: this.pageSize,
          sysParameterId: this.searchForm.sysParameterId,
          startTime: this.searchForm.startTime,
          endTime: this.searchForm.endTime
        }).then(res => {
          if(res.data.code === 2000) {
            this.tableData = res.data.data.records;
            this.total = res.data.data.total;
          }
        })
      },
      // 新增按钮操作
      openAdd() {
        this.dialog.flag = 0;
        this.resetForm("sysParameterForm");
        this.dialog.dialogTitle = "新增";
        this.dialog.visible = true;
      },
      // 更新按钮操作
      openUpdate(row) {
        this.dialog.flag = 1;
        this.resetForm("sysParameterForm");
        this.dialog.dialogTitle = "编辑";
        this.dialog.visible = true;
        this.getById(row.sysParameterId);
      },
      // 提交数据操作
      submitFun() {
        this.$refs.sysParameterForm.validate(valid => {
          if(valid){
            this.loading.status = true;
            this.$api.sys_sysParameter.saveOrUpdate(this.sysParameterForm,this.dialog.flag).then(res => {
              if (res.data.code === 2000) {
                this.$message.success(res.data.msg);
                if(this.dialog.flag === 0){
                  this.pageNum = 1;
                }
                this.getDataList();
                // 关闭弹框
                this.dialog.visible = false;
                // 关闭加载
                this.loading.status = false;
              }else{
                this.$message.error(res.data.msg);
                this.loading.status = false;
                return false;
              }
            })
          }
        })
      },
      // 根据id获取信息
      getById(id) {
        this.$api.sys_sysParameter.getById(id).then(res => {
          if (res.data.code === 2000) {
            this.sysParameterForm.sysParameterId = res.data.data.sysParameterId;
            this.sysParameterForm.sysCompanyId = res.data.data.sysCompanyId;
            this.sysParameterForm.parameterName = res.data.data.parameterName;
            this.sysParameterForm.dataValue = res.data.data.dataValue;
            this.sysParameterForm.parameterType = res.data.data.parameterType;
            this.sysParameterForm.dataType = res.data.data.dataType;
            this.sysParameterForm.msg = res.data.data.msg;
            this.sysParameterForm.numb = res.data.data.numb;
            this.sysParameterForm.seq = res.data.data.seq;
            this.sysParameterForm.deleted = res.data.data.deleted;
          }
        });
      },
      // 根据id删除
      deleteFun(row) {
        this.$confirm("确定删除当前吗？", "系统提示", {confirmButtonText: "确定", cancelButtonText: "取消", type: 'error'
        }).then(() =>{
          this.$api.sys_sysParameter.remove(row.sysParameterId).then(res => {
            if(res.data.code === 2000){
              this.$message.success(res.data.msg);
              this.getDataList()
            }else {
              this.$message.error(res.data.msg);
            }
          })
        }).catch(() => {
          this.$message.info("取消删除");
        });
      },
      // 清空表单
      resetForm(formName) {
        if (this.$refs[formName]) {
          this.$refs[formName].resetFields();
        }
      },
    }
  }
</script>
<style lang="less" scoped>
  .el-search-item {
    margin-bottom: 0;
  }
  .el-card/deep/.el-card__body {
    padding: 0 10px 10px 10px !important;
  }
  .el-card/deep/.el-card__header {
    border-bottom: none !important;padding: 10px 10px !important;
  }
</style>